package com.tianzheng.rzzl.base1

import com.basic.library.utils.JsonUtils
import com.google.gson.Gson
import okhttp3.internal.and
import java.io.BufferedReader
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.URL
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException


fun send() {
    val appkey = "622991c0ffd298390aad7e19"
    val app_master_secret = "x62ziujl4fwxa1hlqz4ead3xbz7mdmqd"
    val timestamp = System.currentTimeMillis()
    val method = "POST"
    val url = "http://msg.umeng.com/api/send"
    var params =
            "{\"appkey\":\"$appkey\",\"timestamp\":$timestamp,\"description\":\"测试\",\"payload\":{\"display_type\":\"notification\",\"body\":{\"ticker\":\"标题\",\"text\":\"内容\",\"title\":\"标题\",\"after_open\":\"go_app\",\"play_sound\":true}},\"type\":\"customizedcast\",\"alias_type\":\"PushId\",\"alias\":\"83e36916-2779-4e96-b3c3-fb414fa734a0\",\"mipush\":true,\"mi_activity\":\"com.tz.tzoa.MfrMessageActivity\",\"policy\":{\"expire_time\":\"2022-07-25 18:45:34\"}}"
    params = "{appkey = \"622991c0ffd298390aad7e19\", timestamp = \"1658382375\", type = \"customizedcast\", alias_type = \"PushId\", alias = \"06d2c454-bd54-4519-8782-ec79435d530d\", payload = { display_type = \"notification\", body = { R = \"\", title = \"系统消息\", text = \"朱恩民给您指派了一条电话工单，【工单编号：210D-220721-002】请及时处理！\" }, extra = { MessageCategory = \"workflow\", ObjectCategory = \"workflow\", ObjectId = 336 } }, description = \"系统消息推送\" }"
    val map = JsonUtils.fromJson(params, PushInfo1::class.java)
    map.alias = "56153793-9749-4da6-8a4f-6ddf9e0b855c"
    map.timestamp = timestamp
    val sStr = "$method$url${Gson().toJson(map)}$app_master_secret"
    println(sStr)
    val sign = encoder(sStr)
    println(sign)
    val mde = "$url?sign=$sign"
    println(mde)

    val postUrl = URL(mde)
    val conn = postUrl.openConnection() as HttpURLConnection
    conn.doInput = true
    conn.doOutput = true
    conn.useCaches = false
    conn.setRequestProperty("Content-type", "application/json")
    conn.instanceFollowRedirects = false
    conn.requestMethod = "POST"
    conn.connect()
    val bytes = Gson().toJson(map).toByteArray()
    val outputStream = conn.outputStream
    outputStream.write(bytes)
    outputStream.flush()
    outputStream.close()
    val buffer = StringBuffer()

    if (conn.responseCode == 200) {
        val input = BufferedReader(InputStreamReader(conn.getInputStream(), "utf-8")) //防止乱码
        var inputLine: String? = null
        while (input.readLine().also { inputLine = it } != null) {
            buffer.append(inputLine)
        }
        println(buffer.toString())
    }

//    RHttpManager.obj().postbodyAsyn(null, mde, map,
//            object : RHttpManager.ResultCallback<DataResult<UserInfo>>() {
//                override fun onResponse(response: DataResult<UserInfo>) {
//
//                }
//            })
}

fun main() {
    send()
}

fun encoder(psd: String): String? {
    try {
        //1,指定加密算法类型
        val digest = MessageDigest.getInstance("MD5")

        //2,将需要加密的字符串中转换成byte类型的数组,然后进行随机哈希过程
        val bs = digest.digest(psd.toByteArray())

        //3,循环遍历bs,然后让其生成32位字符串,固定写法
        //4,拼接字符串过程
        val stringBuffer = StringBuffer()
        for (b in bs) {
            val i: Int = b and 0xff
            //int类型的i需要转换成16机制字符
            var hexString = Integer.toHexString(i)
            if (hexString.length < 2) {
                hexString = "0$hexString"
            }
            stringBuffer.append(hexString)
        }
        //5,打印测试
        return stringBuffer.toString()
    } catch (e: NoSuchAlgorithmException) {
        e.printStackTrace()
    }
    return ""
}

data class PushInfo1(
    var alias: String,
    var alias_type: String,
    var appkey: String,
    var description: String,
    var mi_activity: String,
    var mipush: Boolean,
    var payload: Payload,
    var policy: Policy,
    var timestamp: Long,
    var type: String
)

data class Payload(
    val body: Body,
    val display_type: String
)

data class Body(
        val after_open: String,
        val play_sound: Boolean,
        val text: String,
        val ticker: String,
        val title: String
)

data class Policy(
        val expire_time: String
)